package com.tange.feature.device.query

import com.tange.core.backend.service.response.RxResponse
import com.tange.core.backend.service.response.RxResponseTransform
import com.tange.feature.data.structure.DeviceBatteryResp
import com.tange.feature.data.structure.DeviceLocationResp
import com.tg.appcommon.android.TGLog
import com.tg.data.http.entity.DeviceServiceStatusBean
import com.tg.network.socket.http.TGHttp
import io.reactivex.Observable

/**
 * 设备周边信息查询
 */
object DeviceAdditionalQuery {

    private const val TAG = "DeviceAdditionalQuery——"
    private const val PARAM_UUID_NAME = "uuid"
    private const val PARAM_UUID_SPLIT = ","

    /** 查询设备服务信息 */
    fun queryService(uuids: List<String>): Observable<RxResponse<HashMap<String, DeviceServiceStatusBean>>> =
        Observable.create<RxResponse<HashMap<String, DeviceServiceStatusBean>>> {
            if (it.isDisposed) return@create
            if (uuids.isEmpty()) {
                TGLog.i(TAG, "[queryService] no devices")
                it.onNext(emptyRxResp())
            } else {
                val params = formatParams(uuids)
                TGHttp.getInstance().getDeviceService(params).subscribe(RxResponseTransform(it))
            }
        }.map {
            TGLog.i(TAG, "[queryService] ret = $it")
            return@map it
        }

    /** 查询设备定位信息 */
    fun queryLocation(uuids: List<String>): Observable<RxResponse<HashMap<String, DeviceLocationResp>>> =
        Observable.create<RxResponse<HashMap<String, DeviceLocationResp>>> {
            if (it.isDisposed) return@create
            if (uuids.isEmpty()) {
                TGLog.i(TAG, "[queryLocation] no devices")
                it.onNext(emptyRxResp())
            } else {
                val params = formatParams(uuids)
                TGHttp.getInstance().getDeviceLocation(params).subscribe(RxResponseTransform(it))
            }
        }.map {
            TGLog.i(TAG, "[queryLocation] ret = $it")
            return@map it
        }

    /** 查询设备电量信息 */
    fun queryBattery(uuids: List<String>): Observable<RxResponse<HashMap<String, DeviceBatteryResp>>> =
        Observable.create<RxResponse<HashMap<String, DeviceBatteryResp>>> {
            if (it.isDisposed) return@create
            if (uuids.isEmpty()) {
                TGLog.i(TAG, "[queryBattery] no devices")
                it.onNext(emptyRxResp())
            } else {
                val params = formatParams(uuids)
                TGHttp.getInstance().getDeviceBattery(params).subscribe(RxResponseTransform(it))
            }
        }.map {
            TGLog.i(TAG, "[queryBattery] ret = $it")
            return@map it
        }

    private fun <T> emptyRxResp(): RxResponse<T> = RxResponse<T>().apply {
        isSuccess = true
    }

    private fun formatParams(uuids: List<String>): HashMap<String, String> {
        val params = HashMap<String, String>()
        val builder = StringBuilder()
        uuids.forEach { builder.append(it).append(PARAM_UUID_SPLIT) }
        params[PARAM_UUID_NAME] = builder.toString()
        return params
    }
}